﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BSPortal.Usercontrol.Admin.Book.Order
{
    public partial class frmOrderDetail : System.Web.UI.Page
    {
        private short id = 0;
        int _pageIndex = 1;
        MainService service = new MainService();
        public short OrderId;
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["OrderId"];
            if (id != null)
            {
                OrderId = Convert.ToInt16(id);
            }
            LoadData();

            if (!IsPostBack)
            {
                LoadCity();
                LoadDistrict();
                LoadWard();

                LoadData2();
            }
        }

        #region Load Data
        void LoadData()
        {
            bindData();
        }
        #endregion

        #region Grid Event
        protected void bindData()
        {
            try
            {
                var data = service.GetListOrderDetailClassByOrderId(OrderId);
                RadGrid.DataSource = data;
                RadGrid.VirtualItemCount = data.Count;
                RadGrid.PageSize = PageConst.PAGES_SIZE;
                RadGrid.DataBind();
                
            }
            catch (Exception exception)
            {

            }
        }

        protected void RadGrid_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
        {
            _pageIndex = e.NewPageIndex + 1;
        }

        protected void RadGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                if ((e.Item is GridEditableItem && e.Item.IsInEditMode))//for remove duplicate caption and edit form template
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    try
                    {
                        item.Controls[1].Controls[0].Controls[1].Controls[0].Controls[0].Visible = false;
                    }
                    catch
                    {
                    }
                }

                if (e.Item is GridDataItem)//for item template
                {
                    if (e.Item is GridDataItem)//for item template
                    {
                        Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                        if (lblSTT != null)
                            lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                    }
                    Image imgUpdate = (Image)e.Item.FindControl("imgUpdateDetail");
                    Image imgDelete = (Image)e.Item.FindControl("imgDeleteDetail");

                    string Id = ((OrderDetailClass)e.Item.DataItem).OrderDetailId.ToString();
                    string script = string.Format("document.getElementById('{0}').value='{1}'; return true;", hdId.ClientID, Id);
                    string script2 = string.Format("document.getElementById('{0}').value='{1}'; if(confirm('Xóa sản phẩm này ?')) return true; else return false;", hdId.ClientID, Id);

                    imgUpdate.Attributes.Add("onclick", script);
                    imgDelete.Attributes.Add("onclick", script2);
                }
            }
            catch (Exception)
            {
            }
        }

        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            bindData();
        }

        protected void imgAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            string id = Request.QueryString["OrderId"];
            RadWindow frmProductPriceNewsWindow = frmProductPriceNews;
            frmProductPriceNews.Height = 300;
            frmProductPriceNews.Title = "Thêm Chi tiết";
            frmProductPriceNewsWindow.NavigateUrl = "frmOrderDetailOfDetail.aspx?OrderId=" + id;
            frmProductPriceNewsWindow.Visible = true;
            frmProductPriceNewsWindow.Width = 500;
            frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }



        protected void imgUpdate_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            string id = Request.QueryString["OrderId"];
            RadWindow frmProductPriceNewsWindow = frmProductPriceNews;
            frmProductPriceNews.Height = 300;
            frmProductPriceNewsWindow.Title = "Chỉnh sửa thông tin đơn đặt hàng";
            frmProductPriceNewsWindow.NavigateUrl = "frmOrderDetailOfDetail.aspx?OrderId=" + id + "&OrderDetailId=" + hdId.Value;
            frmProductPriceNewsWindow.Visible = true;
            frmProductPriceNewsWindow.Width = 500;
            frmProductPriceNewsWindow.KeepInScreenBounds = false;
            frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }

        protected void imgDelete_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            MainService service = Common.GetClientService();
            int retcode = service.DeleteOrderDetail(short.Parse(hdId.Value));
            if (retcode == (int)Common.RetCode.SUCESS)
            {
                bindData();
            }
        }
        #endregion

        #region Click

        protected void btnSave_Click(object sender, EventArgs e)
        {
            
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            //txtName.Text = string.Empty;
            //txtPrice.Text = string.Empty;
            //ddlParentCategory.SelectedIndex = 0;
            //ddlPriceType.SelectedIndex = 0;
            //ddlCity.SelectedIndex = 0;
        }

        protected void btnReLoad_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            RadGrid.CurrentPageIndex = 0;
            _pageIndex = 1;
            bindData();
            
        }
        #endregion    

        protected void LoadCity()
        {
            var mainService = Common.GetClientService();
            var listCity = mainService.GetAllCity().OrderBy(row => row.Name);

            ddlCity.DataValueField = "CityId";
            ddlCity.DataTextField = "Name";
            ddlCity.DataSource = listCity;
            ddlCity.DataBind();
        }

        protected void LoadDistrict()
        {
            if (ddlCity.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listDistrict = mainService.GetAllDistrict(int.Parse(ddlCity.SelectedValue)).OrderBy(row => row.Name);

                ddlDistrict.DataValueField = "DistrictId";
                ddlDistrict.DataTextField = "Name";
                ddlDistrict.DataSource = listDistrict;
                ddlDistrict.DataBind();
            }
        }

        protected void LoadWard()
        {
            if (ddlDistrict.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listWard =
                    mainService.GetAllWard(int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue)).OrderBy(row => row.Name);

                ddlWard.DataValueField = "WardId";
                ddlWard.DataTextField = "Name";
                ddlWard.DataSource = listWard;
                ddlWard.DataBind();
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDistrict();
            ddlDistrict_SelectedIndexChanged(sender, e);
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadWard();
        }

        void LoadData2()
        {
            string str = Request["OrderId"];
            if (str == null)
            {


            }
            else
            {
                id = short.Parse(str);
                var data = service.GetOrderById(short.Parse(str));
                if (data != null)
                {
                    txtId.Text = data.OrderId.ToString();
                    txtAddress.Text = data.Address;
                    txtFee.Text = data.Fee.Value.ToString();
                    txtPhone.Text = data.Phone.ToString();
                    ddlStatus.SelectedValue = data.Status.Value.ToString();
                    txtNote.Text = data.Note.Trim();
                    ddlCity.SelectedValue = data.City.ToString();
                    ddlCity_SelectedIndexChanged(null, null);
                    ddlDistrict.SelectedValue = data.District.ToString();
                    ddlDistrict_SelectedIndexChanged(null, null);
                    ddlWard.SelectedValue = data.Ward.ToString();
                }

            }
        }

        protected void btnSaveInfo_Click(object sender, EventArgs e)
        {

            string str = Request["OrderId"];

            if (str == null)
            {

            }
            else
            {
                var data = service.GetOrderById(short.Parse(str));
                hdModifiedInfo.Value = "true";
                int retcode = service.UpdateOrder(short.Parse(str), data.Name, txtAddress.Text, txtNote.Text.Trim(), txtPhone.Text,
                                                data.TransportType, float.Parse(txtFee.Text), (int)data.PayType, int.Parse(ddlStatus.SelectedValue),
                                                int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue), int.Parse(ddlWard.SelectedValue));
                lblStatus.Text = retcode == (int)Common.RetCode.SUCESS ? "Cập nhật thành công..." : "Cập nhật không thành công...";
            }
        }

        protected void btnResetInfo_Click(object sender, EventArgs e)
        {
            txtFee.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtPhone.Text = string.Empty;
            ddlStatus.SelectedIndex = 0;
            txtNote.Text = string.Empty;
        }

    }
}
